<template>
  <div>
    <!-- 添加或修改股票基础信息对话框 -->
    <el-dialog :title="title" :visible.sync="addOrUpdateParam.open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="股票code" prop="stockCode">
          <el-input v-model="form.stockCode" placeholder="请输入股票code"/>
        </el-form-item>
        <el-form-item label="股票名称" prop="stockName">
          <el-input v-model="form.stockName" placeholder="请输入股票名称"/>
        </el-form-item>
        <el-form-item label="所属行业" prop="industry">
          <el-select v-model="form.industry">
            <el-option v-for="item in industryOptions" :key="item.name" :label="item.name" :value="item.name"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="交易所" prop="stockExchange">
          <el-input v-model="form.stockExchange" placeholder="交易所"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getStock, addStock, updateStock } from '../../../api/eye/stock'
import { industryOptions } from '../../../api/eye/industry'

export default {
  name: 'Stock',
  props: {
    addOrUpdateParam: {
      type: Object
    }
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      industryOptions: [],
      // 弹出层标题
      title: '',
      // 表单参数
      form: { id: '' },
      // 表单校验
      rules: {}
    }
  },
  created() {
    industryOptions().then(response => {
      console.log('子组件created')
      this.industryOptions = response.rows
    })
  },
  methods: {
    /** 添加股票提交按钮 */
    submitForm() {

      if (this.form.id) {
        console.log('修改窗口')
        updateStock(this.form).then(response => {
          this.$modal.msgSuccess(response.msg)
          this.addOrUpdateParam.open = false
          // 调用父组件的方法
          this.$emit('getList')
        })
      } else {
        console.log('添加窗口')
        this.title = '添加股票基础信息'
        this.form.stockExchange = 'fund'
        addStock(this.form).then(response => {
          this.$modal.msgSuccess(response.msg)
          this.addOrUpdateParam.open = false
          // 调用父组件的方法
          this.$emit('getList')
        })
      }
    },
    // 取消按钮
    cancel() {
      this.addOrUpdateParam.open = false
    },
    /** 新增按钮操作 */
    handleAdd(id) {
      this.form.id = id
      this.addOrUpdateParam.open = true
      if (id) {
        console.log('修改窗口')
        getStock(id).then(response => {
          this.form = response.data
          if (!this.form.industry) {
            this.form.industry = this.industryOptions[0].name
          }
          this.title = '修改股票基础信息'
        })
      } else {
        console.log('添加窗口')
        this.title = '添加股票基础信息'
        this.form = {}
        this.form.stockExchange = 'fund'
      }

    }
  }
}
</script>

